<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\CountryModel;
use App\MovieModel;
use App\TypeModel;
use Illuminate\Http\Request;
use mysql_xdevapi\Result;

class MovieController extends Controller
{
    public function kino(Request $request)
    {
        $page = $request->input("page");
        $size = 3;
        $offset = ($page-1)*$size;
        $movieCount = MovieModel::count();
        $maxPage = ceil($movieCount/$size);
        $cols = ["movie.*", "type.name_ch as typeName", "country.name_ch as countryName"];
        $movies = MovieModel::leftjoin("type", "movie.type", "type.id")->leftjoin("country", "movie.country", "country.id")->offset($offset)->take($size)->get($cols);
        return view("admin.movie", compact("movies","page","maxPage"));
    }

    public function movieaddpage(Request $request)
    {
        $id = $request->input("id");
        $movie = MovieModel::find($id);
        $types = TypeModel::get();
        $countries = CountryModel::get();
        return view("admin.movieaddpage", compact("types", "countries","movie"));
    }

    public function movieaddInfo(Request $request)
    {

        try {
            $input = $request->input();
            unset($input['_token']);
            if($input['id']==0){
                $img = $request->file("img");
                if ($img) {
                    $input['img'] = $this->fileUploader($img,"image");
                }
                $movie = $request->file("movie");
                if ($movie) {
                    $input['movie'] = $this->fileUploader($movie,"movie");
                }
                $input['score'] *= 10;
                $movie = MovieModel::create($input);
            }else{
                $editMovie = MovieModel::find($input['id']);
                if($request->hasFile("img")){
                    $img = $request->file("img");
                    $input['img'] = $this->fileUploader($img,"image");
                    if(file_exists(public_path($editMovie->img))){
                        unlink(public_path($editMovie->img));
                    }
                }
                if($request->hasFile("movie")){
                    $movie = $request->file("movie");
                    $input['movie'] = $this->fileUploader($movie,"movie");
                    if(file_exists(public_path($editMovie->movie))){
                        unlink(public_path($editMovie->movie));
                    }
                }
                $editMovie->update($input);
            }
            return back()->with("result", "OK");
        } catch (\Exception $e) {
            dd($e->getMessage());
            dd($e->getLine());
//            return back()->with("result", "NO");
        }
    }

    public function moviedelete(Request $request)
    {
        $id = $request->input("id");
        MovieModel::find($id)->delete();
        $result = ["title"=>"添加数据成功！","err"=>0];
        return json_encode($result);
    }

    public function fileUploader($file,$fileType)
    {
        $fileName = $fileType."_" . date("Y_m_d") . "_" . rand(1, 1000) . "." . $file->getClientOriginalExtension();
        $file->move(public_path($fileType), $fileName);
        return $fileType ."/". $fileName;
    }
}
